/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2018年6月9日
 * <修改描述:>
 */
package com.tx.component.servicelogger.support.mybatis;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.tx.component.servicelogger.support.ServiceLogger;
import com.tx.core.exceptions.util.AssertUtils;
import com.tx.core.mybatis.mapper.MybatisBaseMapper;
import com.tx.core.paged.model.PagedList;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 默认的实体自动持久层<br/>
 * <功能详细描述>
 * 
 * @author  Administrator
 * @version  [版本号, 2018年6月9日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class DefaultServiceLoggerImpl<T,ID extends Serializable> implements ServiceLogger<T,ID> {
    
    /** bean类型 */
    private Class<T> beanType;

    private MybatisBaseMapper<T,ID> baseMapper;
    
    /** <默认构造函数> */
    public DefaultServiceLoggerImpl(Class<T> beanType,MybatisBaseMapper<T,ID> baseMapper) {
        super();
        AssertUtils.notNull(beanType, "beanType is null.");
        AssertUtils.notNull(baseMapper, "baseMapper is null.");
        
        this.beanType = beanType;
        this.baseMapper = baseMapper;
    }
    
    /**
     * @return
     */
    @Override
    public Class<T> getEntityType() {
        return this.beanType;
    }
    
    /**
     * @param objectList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(final List<T> objectList) {
        objectList.stream().forEach(obj -> {
            this.baseMapper.insert(obj);
        });
    }
    
    /**
     * @param entity
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(T entity) {
        this.baseMapper.insert(entity);
    }
    
    /**
     * @param pk
     * @return
     */
    @Override
    public T findById(ID pk) {
        return this.baseMapper.findById(pk);
    }
    
    /**
     * @param wrapper
     * @return
     */
    @Override
    public List<T> queryList(Wrapper<T> wrapper) {
        return this.baseMapper.queryList(wrapper);
    }
    
    /**
     * @param wrapper
     * @return
     */
    @Override
    public int count(Wrapper<T> wrapper) {
        return this.baseMapper.count(wrapper).intValue();
    }
    
    /**
     * @param wrapper
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public PagedList<T> queryPagedList(Wrapper<T> wrapper, int pageIndex,
            int pageSize) {
        return this.baseMapper.queryPagedList(wrapper,pageIndex,pageSize);
    }


    public void setBeanType(Class<T> beanType) {
        this.beanType = beanType;
    }

    public void setBaseMapper(MybatisBaseMapper<T, ID> baseMapper) {
        this.baseMapper = baseMapper;
    }
}
